﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class ItemsModels
    {
        public List<Items> GetItems()
        {
            List<Items> listitem = new List<Items>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select*from TblItems";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Items It = new Items();
                    It.ItemID = int.Parse(con.dataRead["ItemID"].ToString());
                    It.ItemName = con.dataRead["ItemName"].ToString();
                    It.IsBind = con.dataRead["IsBind"].ToString();
                    It.Quantity = int.Parse(con.dataRead["Quantity"].ToString());
                    listitem.Add(It);

                }

            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listitem;
        }
        public void CreateItems(Items item)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_CreateItems @ItemName=N'" + item.ItemName + "'" + ", @IsBind=N'" + item.IsBind + "'" + ",@Quantity='" + item.Quantity + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally
            {
                con.CloseData();
                con = null;
            }
        }

        public void EditItems(Items It)
        {
            ConnectDB con=new ConnectDB();
            try
            {
                string sql = "sp_EditItems @ItemID='" + It.ItemID + "'" + ",@ItemName=N'" + It.ItemName + "'" + ", @IsBind=N'" + It.IsBind + "'" + ",@Quantity='" + It.Quantity + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }

        }
        public void DeleteItems(int ItemID)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_DeleteItems @ItemID='"+ItemID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally {
                con.CloseData();
                con = null;
            }
        }
        public List<Items> GetItemID(int ItemID)
        {
            ConnectDB con=new ConnectDB();
            List<Items> listitemid = new List<Items>();
            try
            {
                string sql = "sp_GetItemID @ItemID='" + ItemID + "'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Items It = new Items();
                    It.ItemID =int.Parse(con.dataRead["ItemID"].ToString());
                    It.ItemName = con.dataRead["ItemName"].ToString();
                    It.IsBind = con.dataRead["IsBind"].ToString();
                    It.Quantity=int.Parse(con.dataRead["Quantity"].ToString());
                    listitemid.Add(It);
 
                }
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listitemid;
        }
        public List<Items> SearchItem(string ItemName)
        {
            List<Items> listserachitem = new List<Items>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select ItemID,ItemName,IsBind,Quantity from TblItems where ItemName like N'%" + ItemName + "%'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Items It = new Items();
                    It.ItemID = int.Parse(con.dataRead["ItemID"].ToString());
                    It.ItemName = con.dataRead["ItemName"].ToString();
                    It.IsBind = con.dataRead["IsBind"].ToString();
                    It.Quantity = int.Parse(con.dataRead["Quantity"].ToString());
                    listserachitem.Add(It);

                }

            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listserachitem;

        }

    }
}